package com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bw.liuyushan.mvp_lib.View.BaseActivity;
import com.bw.liuyushan.zhuangao4_mvptest1.Adapter.MainAdapter;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.Contrack.ICTest;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.Model.BaseModelTest;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Fragment.Fragment_Main1;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Fragment.Fragment_Main2;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Fragment.Fragment_Main3;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Fragment.Fragment_Main4;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Fragment.Fragment_Main5;
import com.bw.liuyushan.zhuangao4_mvptest1.R;
import com.bw.liuyushan.zhuangao4_mvptest1.Utils.WindowUtils;
import com.frame.lib_common.slidingmenu.SlidingMenu;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

/**
 * ZhuanGao4_MVPTest1
 * file name is : MainActivity
 * created by Ender on 2021/8/11 14:44.
 * author : Liuyushan
 * Describe: Activity类 就是V层 负责绑定视图显示数据 向P层获取
 */
public class MainActivity extends BaseActivity<BasePresenterTest> implements ICTest.C_ViewTest {
    private androidx.viewpager.widget.ViewPager vpMain;
    private android.widget.RadioGroup rgMain;
    private android.widget.RadioButton bm1;
    private android.widget.RadioButton bm2;
    private android.widget.RadioButton bm3;
    private android.widget.RadioButton bm4;
    private android.widget.RadioButton bm5;
    ArrayList<Fragment> fragments;//Fragment数组
    SharedPreferences sharedPreferences;//sp对象
    SlidingMenu slidingMenu;//侧拉菜单

    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        //沉浸式布局
        WindowUtils.hideStatusBar(this);
        //防止内存泄露
        ip=new BasePresenterTest(new BaseModelTest(),this);
        //EventBus注册
        EventBus.getDefault().register(this);

        vpMain = (ViewPager) findViewById(R.id.vp_Main);
        rgMain = (RadioGroup) findViewById(R.id.rg_Main);
        bm1 = (RadioButton) findViewById(R.id.bm1);
        bm2 = (RadioButton) findViewById(R.id.bm2);
        bm3 = (RadioButton) findViewById(R.id.bm3);
        bm4 = (RadioButton) findViewById(R.id.bm4);
        bm5 = (RadioButton) findViewById(R.id.bm5);
    }

    @Override
    public void initDate() {
    //侧拉菜单
        slidingMenu = new SlidingMenu(this);
        slidingMenu.setBehindWidth(950);
        slidingMenu.setMode(SlidingMenu.LEFT);
        slidingMenu.setMenu(R.layout.slidingmenus);
        slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);//TOUCHMODE_NONE:侧拉菜单不可通过滑动显示
        /**
         * 退出登录的按钮点击事件
         * 点击->清除sp->关闭侧拉菜单->侧拉设置不可拉出模式->EventBus发消息:退出登录
         */
        findViewById(R.id.exit_slidUese).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor edit = sharedPreferences.edit();
                edit.clear();
                edit.commit();
                slidingMenu.toggle();
//                Intent intent = new Intent(MainActivity.this, MainActivity.class);
//                startActivity(intent);
//                sharedPreferences.edit().clear();
                slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
                EventBus.getDefault().postSticky("退出登录");
            }
        });
        /**
         * 侧拉菜单二维码图片点击事件
         * 点击跳转->ZxingActivity->显示二维码
         */
        findViewById(R.id.img_Zxing).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ZxingActivity.class);
                startActivity(intent);
            }
        });

        //初始化Fragment
        fragments = new ArrayList<>();
        fragments.add(new Fragment_Main1());
        fragments.add(new Fragment_Main2());
        fragments.add(new Fragment_Main3());
        fragments.add(new Fragment_Main4());
        fragments.add(new Fragment_Main5());
        //设置适配器
        MainAdapter mainAdapter = new MainAdapter(getSupportFragmentManager(), fragments);
        vpMain.setAdapter(mainAdapter);

        /**
         * RadioButton监听事件 按钮改变切换视图
         */
        rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.bm1:
                        vpMain.setCurrentItem(0);
                        break;
                    case R.id.bm2:
                        vpMain.setCurrentItem(1);
                        break;
                    case R.id.bm3:
                        vpMain.setCurrentItem(2);
                        break;
                    case R.id.bm4:
                        vpMain.setCurrentItem(3);
                        break;
                    case R.id.bm5:
                        vpMain.setCurrentItem(4);
                        break;
                }
            }
        });
        /**
         * 当ViewPage切换时的监听事件 按钮跟随改变
         */
        vpMain.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        rgMain.check(R.id.bm1);
                        break;
                    case 1:
                        rgMain.check(R.id.bm2);
                        break;
                    case 2:
                        rgMain.check(R.id.bm3);
                        break;
                    case 3:
                        rgMain.check(R.id.bm4);
                        break;
                    case 4:
                        rgMain.check(R.id.bm5);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    /**
     * 当布局发生改变时候的方法
     * 沉浸式布局 工具类调用
     * @param params
     */
    @Override
    public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
        super.onWindowAttributesChanged(params);
        WindowUtils.hideStatusBar(this);
    }

    /**
     * EventBus接收值 由Fragment_Main1发出消息
     * sp判断满足已登录状态条件->发送信息->接收到值->弹出侧拉菜单
     * @param slid
     */
    @Subscribe(sticky = false)
    public void EventBusMsg(String slid){
        if (slid.equals("个人信息表")){
            slidingMenu.showContent();
            slidingMenu.showMenu();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //EventBus注销
        EventBus.getDefault().unregister(this);
    }

    /**
     * Activity生命周期 当登录成功之后再次加载主页面Activity
     * onResume内sp判断是否登录 已登录状态下设置可以显示侧拉菜单
     * 头像图片设置圆形
     */
    @Override
    protected void onResume() {
        super.onResume();
        sharedPreferences = getSharedPreferences("sp", MODE_PRIVATE);
        boolean isLogin = sharedPreferences.getBoolean("isLogin", false);
        if (isLogin) {
//            String string = sharedPreferences.getString("LoginUser", "登 录");
//            LoginPage.setText(string);
//            slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
            Glide.with(this).load(R.drawable.fish).circleCrop().into((ImageView) findViewById(R.id.img_slidHead));

        }
    }
}
